from flask import Flask, request, render_template, send_from_directory, jsonify, redirect, url_for
from flask_debug import Debug
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase

class Base(DeclarativeBase):
  pass

db = SQLAlchemy(model_class=Base)

app = Flask(__name__)
Debug(app)

# configure the SQLite database, relative to the app instance folder
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://club:6g54j7g803l73A@rm-j6c6g54j7g803l731ko.mysql.rds.aliyuncs.com/clubactivities"
# initialize the app with the extension
db.init_app(app)

class RinkModel(db.Model):
    __tablename__ = 'rinklist'
    id = db.Column(db.INTEGER, primary_key=True)
    name = db.Column(db.VARCHAR)
    score = db.Column(db.INTEGER)

@app.route('/', methods=['GET'])
def home():
    return render_template("shouye.html")

@app.route('/saishi', methods=['GET'])
def saishi():
    return render_template("saishi.html")

@app.route('/paiming', methods=['GET'])
def paiming():
    rinks = RinkModel.query.all()
    return render_template("paiming.html", rinks=rinks)

@app.route('/me', methods=['GET'])
def me():
    return render_template("me.html")

@app.route('/addrink', methods=['GET','POST'])
def addrink():
    name = request.form.get('name', default='')
    rink = RinkModel(
        name=name,
        score="10"
        )
    db.session.add(rink)
    db.session.commit()
    return redirect('/paiming')

@app.route('/hello', methods=['GET'])
def vision():
    return jsonify({'msg': 'hello world911'})

@app.route('/asset/<path:filename>')
def load_template(filename):
    print(filename)
    return send_from_directory('templates/asset', filename, as_attachment=False)


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=8080)